package com.coship.vasms.adstorm.common;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtils {
	
	public static SimpleDateFormat dateformat = new SimpleDateFormat("MM-dd HH:mm");
	public static SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static SimpleDateFormat dateformatDate = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat dateformatDateTime = new SimpleDateFormat("yy-MM-dd HH:mm");

	 /**
	  * 取得当前时间,格式：HH:mm
	  * @return
	  */
	 public static String getTime() {
		 String time = dateformat1.format(new java.util.Date());
		 return time.substring(11);
	 }
	 
	 /**
	  * 取得当前的时间，格式为：yy-MM-dd HH:mm
	  * @return
	  */
	 public static String getDateTime(){
		 String time = dateformatDateTime.format(new java.util.Date());
		 return time;
	 }
	 
	 /**
	  * 取得当前日期,格式为：yyyy-MM-dd
	  * @return
	  */
	 public static String getDate(){
		 String time = dateformatDate.format(new java.util.Date());
		 return time;
	 }
	 
	 /**
	  * 取得当前的月份和时间
	  * @param d
	  * @return
	  */
	 public static String getTime(java.util.Date d) {
		 String time = dateformat.format(d);
		 return time ;
	 }
	 
	 /**
	  * 取得当月的最后一天
	  * @param in_date
	  * @return
	  */
	 public static String getLastDayinMonth( String in_date ) {
		 StringBuffer sb = new StringBuffer();
		 int year =  Integer.parseInt( in_date.substring( 0, 4 ) );
		 int month = Integer.parseInt( in_date.substring( 5, 7 ) ) ;
		 int day = getLastDayinMonth(year,month);
		 sb.append(year+"-");
		 if(month < 10){
			 sb.append("0");
		 }
		 sb.append(month +"-");
		 if(day<10){
			 sb.append("0");
		 }
		 sb.append(day);
		 return sb.toString();
	 }
	 /**
	  * 取得本月的每一天
	  * 
	  * @param in_date
	  * @return
	  */
	 
	 public static String getFirstDayinMonth( String in_date ) {
		 StringBuffer sb = new StringBuffer();

	 	sb.append(in_date.substring(0,8));
	 	sb.append("01");
	 	return sb.toString();
	 }
	 /**
	  * 取得当月的最后一天
	  * @param in_year
	  * @param in_month
	  * @return
	  */
	 
	// -------------------------------------------------------------------------------------------------------------
	 public static int getLastDayinMonth( int in_year, int in_month ) {
		 Calendar cal = Calendar.getInstance();
	  
		 cal.set( in_year, in_month, 1 );
		 return( cal.getActualMaximum( Calendar.DATE ) );
	 }
	 
	 /**
	  * 取得本周的第一天
	  * @return
	  */
	 public static String getWeekFirst(){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(new Date());
		 int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1;
		 cal.add(Calendar.DATE, -day_of_week);
		 return dateformatDate.format(cal.getTime());
	 }
	 
	 /**
	  * 取得本周的最后一天
	  * @return
	  */
	 public static String getWeekLast(){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(new Date());
		 int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1;
		 cal.add(Calendar.DATE, -day_of_week);
		 cal.add(Calendar.DATE, 6);
		  
		 return dateformatDate.format(cal.getTime());
	 }
	 
	 /**
	  * 取得明天
	  * @return
	  */
	 public static String getTomorrow(){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(new Date());  
		 cal.add(Calendar.DAY_OF_MONTH, 1);
		 return dateformatDate.format(cal.getTime());
	 }
	 
	 /**
	  * 取得后天
	  * @return
	  */
	 public static String getAfterTomorrow(){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(new Date());  
		 cal.add(Calendar.DAY_OF_MONTH, 2);
		 return dateformatDate.format(cal.getTime());
	 }
	 
	 /**   
	     * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式   
	     * @param date2 被比较的时间  为空(null)则为当前时间   
	     * @param stype 返回值类型   0为多少天，1为多少个月，2为多少年   
	     * @return   
	     */  
	public static int compareDate(Date date1,Date date2,int stype){   
	    int n = 0;   
	    String date1Str =  dateformatDate.format(date1);
	    String date2Str =  dateformatDate.format(date2);
	    String[] u = {"天","月","年"};   
	    String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd";     
	    SimpleDateFormat df = new SimpleDateFormat(formatStyle);   
	    Calendar c1 = Calendar.getInstance();   
	    Calendar c2 = Calendar.getInstance();   
	    try {   
	        c1.setTime(df.parse(date1Str));   
	        c2.setTime(df.parse(date2Str));   
	    } catch (Exception e3) {   
	        System.out.println("wrong occured");   
	    }   
	    while (!c1.after(c2)) {                     // 循环对比，直到相等，n 就是所要的结果   
	        //list.add(df.format(c1.getTime()));    // 这里可以把间隔的日期存到数组中 打印出来   
	        n++;   
	        if(stype==1){   
	            c1.add(Calendar.MONTH, 1);          // 比较月份，月份+1   
	        }   
	        else{   
	            c1.add(Calendar.DATE, 1);           // 比较天数，日期+1   
	        }   
	    }             
	    n = n-1;             
	    if(stype==2){   
	        n = (int)n/365;   
	    }
	    if(n==0){
	     n=1;
	    }
	    //System.out.println(date1+" -- "+date2+" 相差多少"+u[stype]+":"+n);         
	    return n;   
	} 
	
	 /**
	  * 时间日期处理
	  * 
	  * @param date
	  * @return
	  */
	 public static String formatTime(Date date) {
		 if (date != null) {
			 DateFormat fd = new SimpleDateFormat("MM-dd HH:mm");
			 return fd.format(date);
		 }
		 return "";
	 }


}
